﻿'use strict'

var app = angular.module('toDoApp', []);

app.controller('toDoController', function ($scope, toDoService) {
    toDoService.getToDo().then(function (data) {
        $scope.toDoArray = data;
    }, function () {
        
    });

    $scope.addToDo = function (toDo) {
        toDoService.addToDo(toDo);
    };

    $scope.completeToDo = function (toDo) {
        toDoService.completeToDo(toDo);
    };

    $scope.deleteToDo = function (toDo) {
        toDoService.deleteToDo(toDo);
    };
});

app.factory('toDoService', function ($http, $q) {
    var toDoArray = [];

    var addToDo = function (toDo) {
        var t = {
            toDo: toDo,
            startDate: new Date(),            
            isActive: true
        };

        var deferred = $q.defer();

        $http({
            url: 'https://coralstodoapp.firebaseio.com/messages/.json',
            method: 'POST',
            data: t,
        }).success(function () {
            toDoArray.push(t);
            deferred.resolve(toDoArray);
        }).error(function () {
            deferred.reject();
        });

        return deferred.promise;
    };

    var completeToDo = function (msgObj) {
        msgObj.isActive = !msgObj.isActive;
        msgObj.finishDate = new Date();

        var deferred = $q.defer();

        $http({
            url: 'https://coralstodoapp.firebaseio.com/messages/' + msgObj.iD + '/.json',
            method: 'PUT',
            data: msgObj,
        }).success(function () {
            toDoArray.push(msgObj.isActive.finishDate);            
            deferred.resolve(toDoArray);
        }).error(function () {
            deferred.reject();
        });
        return deferred.promise;
    }

    var deleteToDo = function (msgObj) {
        
        var deferred = $q.defer();

        $http({
            url: 'https://coralstodoapp.firebaseio.com/messages/' + msgObj.iD + '/.json',
            method: 'DELETE',            
        }).success(function (data) {
            var task = toDoArray.indexOf(msgObj)
            toDoArray.splice(task, 1);
            deferred.resolve(toDoArray);
        }).error(function () {
            deferred.reject();
        });
        return deferred.promise;
    }

    var getToDo = function () {

        var deferred = $q.defer();

        $http({
            url: 'https://coralstodoapp.firebaseio.com/messages/.json',
            method: 'GET'
        }).success(function (data) {
            toDoArray.length = 0;            
            for (var i in data) {
                data[i].iD = i;
                toDoArray.push(data[i]);                
            }

            deferred.resolve(toDoArray);
        }).error(function () {
            deferred.reject();
        });

        return deferred.promise;
    }

    return {
        addToDo: addToDo,
        completeToDo: completeToDo,
        getToDo: getToDo,
        deleteToDo: deleteToDo,
    };
});